import axios from "axios";

export default {
    namespaced: true,
    state: {
        id:"",  // 登录用户ID
        name:"",    // 登录用户昵称
        token:"",   // 登录用户的token
        logintime:"",   // 登录时间
        refreshtime:"", // token的刷新时间
    },
    mutations:{
        getinfo(State,payload){
          State.id = payload.id
          State.name = payload.name
          State.token = payload.token
          State.logintime = new Date().getTime()
          State.refreshtime = 60*60*1000
        },
        gettokensuccess(State,payload){
            State.token = payload
            State.refreshtime = 60*60*1000
        },
        gettokenfaild(State){
            State.refreshtime = 60*1000
        },
    },
    actions:{
        login(content,payload){
            content.commit("getinfo",payload)
            setTimeout(()=>{
                content.dispatch("refreshtoken")
            },content.state.refreshtime)
        },
        refreshtoken(content){
            axios.post().then(res=>{
                if(res.data.success){
                    content.commit("gettokensuccess",res.data.token)
                }else{
                    content.commit("gettokenfaild")
                }
            }).catch(err=>{
                content.commit("gettokenfaild")
            })
            setTimeout(()=>{
                content.dispatch("refreshtoken")
            },content.state.refreshtime)
        },
    },
}



